#include "FoodListPanel.h"

FoodListPanel::FoodListPanel(MasterComponent* master, int w, int h) :
	ListPanel(master, w, h) {

	fatIcon = PNGImageFormat().loadFrom(File(T("images/icons/fat.png")));
	carbIcon = PNGImageFormat().loadFrom(File(T("images/icons/carb.png")));
	proteinIcon = PNGImageFormat().loadFrom(File(T("images/icons/protein.png")));
//	fibreIcon = PNGImageFormat().loadFrom(File(T("images/icons/fibre.png")));

	deque<Eatable>& foods = PersistentFoodList::instance().getFoods();
	copy(foods.begin(), foods.end(), inserter(filteredItems, filteredItems.begin()));

	addAndMakeVisible(foodView = new FoodView(fatIcon, carbIcon, proteinIcon));
	foodView->setBounds(35, 94, 5 * width / 7 - 70, height - 123);

	listBox->updateContent();

	backButton->setFromTo(MasterComponent::FOOD_LIST, MasterComponent::MAIN);

	showItemView(0);
	setWantsKeyboardFocus(true);
}

FoodListPanel::~FoodListPanel() {
	if (fatIcon)
		delete fatIcon;
	if (carbIcon)
		delete carbIcon;
	if (proteinIcon)
		delete proteinIcon;
//	if (fibreIcon)
//		delete fibreIcon;
}

void FoodListPanel::paint(Graphics& g) {
	Rectangle r(0, 0, width, height);
	g.setColour(Colours::palegoldenrod);
	g.fillRoundedRectangle(r, 50);
	g.setColour(Colours::grey);
	MiscGraphics::drawRoundedRect(g, r, 50, 1);

	g.setColour(Colours::black);
	g.setFont(Font(Font::getDefaultSansSerifFontName(), 36, Font::plain));
	g.drawFittedText(T("Food List"), 0, 0, width, 100, Justification::centred, 1);

	paintListBox(g);
	foodView->repaint();
}

void FoodListPanel::mouseDown(const MouseEvent& e) {
}

void FoodListPanel::switchPanels(int from, int to) {
	master->switchPanels(from, to);
}

void FoodListPanel::showItemView(int row) {
	foodView->setEatable(&filteredItems[row]);
	foodView->calculateRatios();
	foodView->repaint();
}

bool FoodListPanel::keyPressed(const KeyPress& k) {
	return false;
}

void FoodListPanel::doubleClicked(const MouseEvent& e) {

}

deque<Eatable>& FoodListPanel::getEatableList() {
	return PersistentFoodList::instance().getFoods();
}
